namespace CISLib
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class ManageApply
    {
        public static DataSet GetApply()
        {
            string strSQL = "Select TB_SysUser.LoginName,TB_Application.* from TB_Application left outer join TB_SysUser on TB_Application.userId = TB_SysUser.userId   order by ApplyResult,ApplyDate";
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.DBSetSelect(strSQL);
            return MyDB.DBReturnDataSet();
        }

        public static DataSet GetApply(Guid userId)
        {
            string strSQL = "Select TB_SysUser.LoginName,TB_Application.* from TB_Application left outer join TB_SysUser on TB_Application.userId = TB_SysUser.userId  where TB_Application.UserId=@UserId order by ApplyResult,ApplyDate";
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(userId, "UserId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            return MyDB.DBReturnDataSet();
        }

        public static Apply GetApplyViaId(Guid id)
        {
            SqlDataReader MyReader = null;
            Apply apply = new Apply();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "select * from TB_Application where ApplyId = @ApplyId";
            MyDB.AddParameters(id, "ApplyId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            if (MyReader.Read())
            {
                apply.ApplyId = MyReader.GetGuid(0);
                apply.UserId = MyReader.GetGuid(1);
                apply.CurrentRoleId = MyReader.GetGuid(2);
                apply.ApplyRoleId = MyReader.GetGuid(3);
                apply.CurrentRegionInfo = MyReader.GetString(4);
                apply.CurrentCATInfo = MyReader.GetString(5);
                apply.ApplyRegionInfo = MyReader.GetString(6);
                apply.ApplyCATInfo = MyReader.GetString(7);
                apply.ApplyDate = MyReader.GetDateTime(8);
                apply.ApplyReason = MyReader.GetString(9);
                apply.ApplyResult = MyReader.GetString(10);
                apply.CurrentUserId = MyReader.GetGuid(11);
                apply.CurrentUserCustomers = MyReader.GetString(12);
                apply.ApplyType = MyReader.GetString(13);
                apply.ApplyFromDate = MyReader.GetDateTime(14);
                apply.ApplyToDate = MyReader.GetDateTime(15);
            }
            MyDB.DBClose();
            return apply;
        }

        public static void InsertNewApply(Guid userId, Guid currentRoleId, Guid applyRoleId, string currentProvinceInfo, string currentCatInfo, string applyProvinceInfo, string applyCatInfo, DateTime applyDate, string applyReason, string applyResult, Guid currentUserId, string currentUserCustomers, string applyType, DateTime applyFromDate, DateTime applyToDate)
        {
            Guid applyId = Guid.NewGuid();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(applyId, "ApplyId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(userId, "UserId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(currentRoleId, "CurrentRoleId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(applyRoleId, "ApplyRoleId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(currentProvinceInfo, "CurrentProvinceInfo", SqlDbType.VarChar);
            MyDB.AddParameters(currentCatInfo, "CurrentCatInfo", SqlDbType.VarChar);
            MyDB.AddParameters(applyProvinceInfo, "ApplyProvinceInfo", SqlDbType.VarChar);
            MyDB.AddParameters(applyCatInfo, "ApplyCatInfo", SqlDbType.VarChar);
            MyDB.AddParameters(applyDate, "ApplyDate", SqlDbType.DateTime);
            MyDB.AddParameters(applyReason, "ApplyReason", SqlDbType.VarChar);
            MyDB.AddParameters(applyResult, "ApplyResult", SqlDbType.VarChar);
            MyDB.AddParameters(currentUserId, "CurrentUserId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(currentUserCustomers, "CurrentUserCustomers", SqlDbType.VarChar);
            MyDB.AddParameters(applyType, "ApplyType", SqlDbType.VarChar);
            MyDB.AddParameters(applyFromDate, "ApplyFromTime", SqlDbType.DateTime);
            MyDB.AddParameters(applyToDate, "ApplyToTime", SqlDbType.DateTime);
            string strSQL = "Insert into TB_Application (ApplyId,UserId,CurrentRoleId,ApplyRoleId,CurrentProvinceInfo,CurrentCatInfo,ApplyProvinceInfo,ApplyCatInfo,ApplyDate,ApplyReason,ApplyResult,CurrentUserId,CurrentUserCustomers,ApplyType,ApplyFromTime,ApplyToTime) values (@ApplyId,@UserId,@CurrentRoleId,@ApplyRoleId,@CurrentProvinceInfo,@CurrentCatInfo,@ApplyProvinceInfo,@ApplyCatInfo,@ApplyDate,@ApplyReason,@ApplyResult,@CurrentUserId,@CurrentUserCustomers,@ApplyType,@ApplyFromTime,@ApplyToTime)";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void RemoveApplyViaId(Guid id)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(id, "ApplyId", SqlDbType.UniqueIdentifier);
            string strSQL = "Delete From TB_Application where ApplyId = @ApplyId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void UpdateAdminApply(Guid applyId, string applyResult)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(applyId, "ApplyId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(applyResult, "ApplyResult", SqlDbType.VarChar);
            string strSQL = "Update TB_Application set ApplyResult=@ApplyResult where ApplyId = @ApplyId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void UpdateApply(Guid applyId, Guid userId, Guid currentRoleId, Guid applyRoleId, string currentProvinceInfo, string currentCatInfo, string applyProvinceInfo, string applyCatInfo, DateTime applyDate, string applyReason, string applyResult, Guid currentUserId, string currentUserCustomers, string applyType, DateTime applyFromDate, DateTime applyToDate)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(applyId, "ApplyId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(userId, "UserId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(currentRoleId, "CurrentRoleId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(applyRoleId, "ApplyRoleId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(currentProvinceInfo, "CurrentProvinceInfo", SqlDbType.VarChar);
            MyDB.AddParameters(currentCatInfo, "CurrentCatInfo", SqlDbType.VarChar);
            MyDB.AddParameters(applyProvinceInfo, "ApplyProvinceInfo", SqlDbType.VarChar);
            MyDB.AddParameters(applyCatInfo, "ApplyCatInfo", SqlDbType.VarChar);
            MyDB.AddParameters(applyDate, "ApplyDate", SqlDbType.DateTime);
            MyDB.AddParameters(applyReason, "ApplyReason", SqlDbType.VarChar);
            MyDB.AddParameters(applyResult, "ApplyResult", SqlDbType.VarChar);
            MyDB.AddParameters(currentUserId, "CurrentUserId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(currentUserCustomers, "CurrentUserCustomers", SqlDbType.VarChar);
            MyDB.AddParameters(applyType, "ApplyType", SqlDbType.VarChar);
            MyDB.AddParameters(applyFromDate, "ApplyFromTime", SqlDbType.DateTime);
            MyDB.AddParameters(applyToDate, "ApplyToTime", SqlDbType.DateTime);
            string strSQL = "Update TB_Application set UserId=@UserId,CurrentRoleId=@CurrentRoleId,ApplyRoleId=@ApplyRoleId,CurrentProvinceInfo=@CurrentProvinceInfo,CurrentCatInfo=@CurrentCatInfo,ApplyProvinceInfo=@ApplyProvinceInfo,ApplyCatInfo=@ApplyCatInfo,ApplyDate=@ApplyDate,ApplyReason=@ApplyReason,ApplyResult=@ApplyResult,CurrentUserId=@CurrentUserId,CurrentUserCustomers=@CurrentUserCustomers,ApplyType=@ApplyType,ApplyFromTime=@ApplyFromTime,ApplyToTime=@ApplyToTime where ApplyId = @ApplyId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }
    }
}

